package com.smartlbs.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.smartlbs.baseframe.utils.ConfigFileUtils;
import com.smartlbs.baseframe.utils.HttpClientUtil;
import com.smartlbs.baseframe.web.ResponseInfo;
import com.smartlbs.entity.BetInfo;
import com.smartlbs.entity.Bigchamp;
import com.smartlbs.entity.GameInfo;
import com.smartlbs.entity.OtherBet;
import com.smartlbs.entity.YunXin;
import com.smartlbs.entity.OtherBet.GE;
import com.smartlbs.entity.SysUser;
import com.smartlbs.service.IBetInfoService;
import com.smartlbs.service.IGameInfoService;
import com.smartlbs.vo.BetInfoVo;
import com.smartlbs.vo.GameInfoVo;

/**
 * 赛事信息控制层类
 * 
 * @author idaowei
 *
 */
@Controller
@RequestMapping("adminuser/gameinfo")
public class SysGameInfoController {
	private static final String SysUser = null;
	@Autowired
	private IGameInfoService gameInfoService;
	@Autowired
	private IBetInfoService betInfoService;

	/**
	 * 根据赛事修改押注赔率信息
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping("updategameinfo")
	@ResponseBody
	public ResponseInfo updateGameinfo(HttpServletRequest request,
			GameInfoVo vos) {
		int result = 0;
		String message = "";
		Object data = null;
		String parameter = request.getParameter("gameinfo");
		GameInfoVo info = JSON.parseObject(parameter, GameInfoVo.class);
		if (info != null) {
			try {
				List<BetInfoVo> betInfoVo = info.getBetInfoVo();
				for (BetInfoVo vo : betInfoVo) {
					BetInfo betInfo = new BetInfo();
					GE bet_obj = vo.getGe();
					Object json = JSONObject.toJSONString(bet_obj,
							SerializerFeature.WriteMapNullValue);
					BeanUtils.copyProperties(betInfo, vo);
					betInfo.setBetInfo(json.toString());
					int update = betInfoService.update(betInfo);
					if (update > 0) {
						result = 1;
						message = "修改成功";
					}
				}
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				message = "发生异常";
			}
		}

		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setData(data);
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		return responseInfo;

	}

	/**
	 * @param request
	 * @param time
	 *            :2019-5-30
	 * @return
	 */
	@RequestMapping("getchampBydate")
	@ResponseBody
	public ResponseInfo getchampBydate(HttpServletRequest request, String time) {
		int result = 0;
		String message = "";
		Object data = null;
		try {
			List<GameInfoVo> findbyTime = gameInfoService.findbyTime(time);
			result = 1;
			message = "获取数据成功";
			data = findbyTime;
		} catch (Exception e) {
			message = "发生异常";
			e.printStackTrace();
			// TODO: handle exception
		}
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setData(data);
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		return responseInfo;
	}

	/**
	 * @param request
	 * @return 大赛事数据
	 */
	@RequestMapping("getbigchamp")
	@ResponseBody
	public ResponseInfo getBigchamp(HttpServletRequest request) {
		String gameName = request.getParameter("gameName");
		String orderStr = request.getParameter("order");
		String startDate = request.getParameter("startDate");
		String endDate = request.getParameter("endDate");
		
		int result = 0;
		String message = "";
		Object data = null;
		
		try {
			List<Bigchamp> findBigchamp = gameInfoService.sysFindBigchamp( gameName, orderStr,startDate, endDate);
			result = 1;
			message = "数据获取成功";
			data = findBigchamp;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			message = "发生异常";
			e.printStackTrace();
		}
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setData(data);
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		return responseInfo;
	}

	/**
	 * 开启关闭比赛
	 * 
	 * @param request
	 * @param status
	 * @param gameId
	 * @return
	 */
	@RequestMapping("beigngame")
	@ResponseBody
	public ResponseInfo beignGame(HttpServletRequest request, Integer status,
			Long gameId) {
		int result = 0;
		String message = "";
		Object data = null;
		GameInfo info = new GameInfo();
		try {
			info.setInfoId(gameId);
			info.setStatus(status);
			int update = gameInfoService.update(info);
			if (update > 0) {
				result = 1;
				message = "开启/关闭成功";
			}
		} catch (Exception e) {
			message = "发生异常";
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setData(data);
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		return responseInfo;
	}

	/**
	 * @param request
	 * @param outid
	 *            大赛事外部id
	 * @return 赛事列表 与具体押注项
	 */
	@RequestMapping("findbybichampname")
	@ResponseBody
	public ResponseInfo findBybichampname(HttpServletRequest request,
			String outid) {
		int result = 0;
		String message = "";
		Object data = null;
		try {
			
			SysUser user = (SysUser) request.getSession().getAttribute(
					"sysUser");
			List<GameInfoVo> findBybichampname = gameInfoService
					.findByoutIdandBet(outid);
			result = 1;
			message = "获取成功";
			data = findBybichampname;
		} catch (Exception e) {
			message = "发生异常";
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		ResponseInfo responseInfo = new ResponseInfo();
		responseInfo.setData(data);
		responseInfo.setResult(result);
		responseInfo.setMessage(message);
		return responseInfo;
	}

	/**
	 * excel 添加赛事
	 * 
	 * @param file
	 * @return
	 */
	@RequestMapping("byfile")
	@ResponseBody
	public ResponseInfo updateByfile(MultipartFile file) {
		ResponseInfo responseInfo = new ResponseInfo();
		String message = "";
		int result = 0;
		Object data = null;
		try {
			Map<String, Object> map = gameInfoService.insertByfile(file);
			result = (int) map.get("result");
			ConfigFileUtils.mobileExcel(file);
			if (result > 0) {
				result = 1;
				message = "上传成功";
			} else {
			//	message = (String) map.get("message");
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			message = "发生异常";
			e.printStackTrace();
		}
		responseInfo.setData(data);
		responseInfo.setMessage(message);
		responseInfo.setResult(result);
		return responseInfo;
	}
	/**	更新词典
	 * @param file
	 * @return
	 */
	@RequestMapping("mobilexcel")
	@ResponseBody
	public ResponseInfo mobileExcel(MultipartFile file){
		ResponseInfo responseInfo = new ResponseInfo();
		String message = "";
		int result = 0;
		Object data = null;
		try {
			
			result  = ConfigFileUtils.mobileExcel(file);
			message = "上传成功";
			if (result <0) {
				
				message = "请查看上传文件是否有误!";
			} 
		} catch (Exception e) {
			// TODO Auto-generated catch block
			message = "上传失败";
			e.printStackTrace();
		}
		responseInfo.setData(data);
		responseInfo.setMessage(message);
		responseInfo.setResult(result);
		return responseInfo;
	}
	/** 赛事信息 英译中
	 * @param request
	 * @param outid
	 * @return
	 */
	@RequestMapping("changeGameName")
	@ResponseBody
	public ResponseInfo changeGameName(HttpServletRequest request,String outid){
		ResponseInfo responseInfo = new ResponseInfo();
		String message = "";
		int result = 0;
		Object data = null;
		try {
			gameInfoService.changeGameName(outid);
			message = "修改成功";
			result=1;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			message = "修改失败";
			e.printStackTrace();
		}
		responseInfo.setData(data);
		responseInfo.setMessage(message);
		responseInfo.setResult(result);
		return responseInfo;
	}
}
